package com.digitalchina.lxnsy.viewholder;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.old.EvaluateActivity;
import com.digitalchina.lxnsy.activity.old.EvaluateDetailsActivity;
import com.digitalchina.lxnsy.api.BusinessOrders;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.CancelReasonVO;
import com.digitalchina.lxnsy.entity.OrderListVO;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.ToastUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;

import java.util.List;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.viewholder
 * 创建人：郑皓
 * 创建时间：2017/1/18 14:47
 * 修改备注：
 * ================================================
 */
public class OrderListHolder extends BaseViewHolder<OrderListVO> {


    private SimpleDraweeView itemOrderSdvMerchantLogo;
    private TextView itemOrderTvMerchantName;
    private TextView itemOrderTvStatus;
    private LinearLayout itemOrderLayoutGoods;
    private TextView itemOrderTvGoodsNumber;
    private TextView itemOrderTvGoodsPrice;
    private TextView itemOrderTvLeftButton;
    private TextView itemOrderTvRightButton;

    private String orderNo = "";

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG.MSG_SELECT_ORDER_CANCEL_REASON_TAG: // 选择取消原因提交
                    BusinessOrders.cancelOrder((Activity) getContext(), orderNo, ((CancelReasonVO) msg.obj).getCancelReason(), mHandler);
                    break;
                case MSG.MSG_CANCEL_ORDER_SUCCESS: // 订单取消成功
                    showToast("订单已取消");
//                    orderListVO.setEqualStatus(24);
//                    setLeftAndRightButtonView(orderListVO);
//                    ordersDetailTvOrderStatus.setText("已取消"); // 订单状态
                    break;
                case MSG.MSG_CANCEL_ORDER_FIELD:
                    showToast(msg.obj.toString());
                    break;
                case MSG.MSG_DELETE_ORDER_SUCCESS:
                    showToast("订单已删除");
                    break;
                case MSG.MSG_DELETE_ORDER_FIELD:
                    showToast(msg.obj.toString());
                    break;
            }
        }
    };

    public OrderListHolder(ViewGroup parent) {
        super(parent, R.layout.item_order_list);

        itemOrderSdvMerchantLogo = $(R.id.item_order_sdv_merchant_logo);
        itemOrderTvMerchantName = $(R.id.item_order_tv_merchant_name);
        itemOrderTvStatus = $(R.id.item_order_tv_status);
        itemOrderLayoutGoods = $(R.id.item_order_layout_goods);
        itemOrderTvGoodsNumber = $(R.id.item_order_tv_goods_number);
        itemOrderTvGoodsPrice = $(R.id.item_order_tv_goods_price);
        itemOrderTvLeftButton = $(R.id.item_order_tv_left_button);
        itemOrderTvRightButton = $(R.id.item_order_tv_right_button);

    }

    @Override
    public void setData(OrderListVO data) {
        super.setData(data);

        // 商户logo
        FrescoUtil.showImageSmall(data.getSellerLogo() != null ? data.getSellerLogo() : "", itemOrderSdvMerchantLogo);
        // 商户名称
        itemOrderTvMerchantName.setText(data.getSellerName());
        // 订单状态
        itemOrderTvStatus.setText(data.getEqualStatusDesc());
        // 商品数量
        itemOrderTvGoodsNumber.setText("共" + data.getOrderItems().size() + "件商品");
        // 商品总价
        itemOrderTvGoodsPrice.setText(data.getTotalPriceReal());


        setLeftAndRightButtonView(data);

        setGoodsItemView(data.getOrderItems());

    }

    /**
     * 设置商品item
     *
     * @param plist
     */
    private void setGoodsItemView(List<OrderListVO.OrderItemsBean> plist) {

        itemOrderLayoutGoods.removeAllViews();
        for (int i = 0; i < plist.size(); i++) {
            View view = LayoutInflater.from(getContext()).inflate(R.layout.item_orders_details_child, null);
            SimpleDraweeView sdvGoodsImage = (SimpleDraweeView) view.findViewById(R.id.item_orders_details_child_iv_images);
            TextView tvGoodsTitle = (TextView) view.findViewById(R.id.item_orders_details_child_tv_title);
            TextView tvGoodsIntroduce = (TextView) view.findViewById(R.id.item_orders_details_child_tv_introduce);
            TextView tvGoodsPrice = (TextView) view.findViewById(R.id.item_orders_details_child_tv_price);


            FrescoUtil.showImageSmall(plist.get(i).getProductImg() != null ? plist.get(i).getProductImg() : "", sdvGoodsImage);
            tvGoodsTitle.setText(plist.get(i).getProductName());
            tvGoodsIntroduce.setText(plist.get(i).getProductDesc());
            tvGoodsPrice.setText(plist.get(i).getPrice());

            itemOrderLayoutGoods.addView(view);
        }
    }

    /**
     * 设置左右安装及监听
     *
     * @param data
     */
    private void setLeftAndRightButtonView(final OrderListVO data) {

        switch (data.getEqualStatus()) {
            case 29: // 待付款
                // 左边按钮
                itemOrderTvLeftButton.setText("取消订单");
                itemOrderTvLeftButton.setVisibility(View.VISIBLE);
                // 右边按钮
                itemOrderTvRightButton.setText("去  支  付");

                itemOrderTvLeftButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderNo = data.getOrderNo();
                        CustomDialog.orderCancelReasonDialog((Activity) getContext(), mHandler);
                    }
                });

                itemOrderTvRightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //GotoUtil.gotoActivity((Activity) getContext(), WXPayEntryActivity.class, "OrderListVO", data);
                    }
                });
                break;
            case 22: // 待评价
                // 左边按钮
                itemOrderTvLeftButton.setVisibility(View.GONE);
                // 右边按钮
                itemOrderTvRightButton.setText("去  评  价");

                itemOrderTvRightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GotoUtil.gotoActivity((Activity) getContext(), EvaluateActivity.class, "OrderListVO", data);
                    }
                });
                break;
            case 23: // 已评价
                // 左边按钮
                itemOrderTvLeftButton.setVisibility(View.GONE);
                // 右边按钮
                itemOrderTvRightButton.setText("查看评价");

                itemOrderTvRightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        GotoUtil.gotoActivity((Activity) getContext(), EvaluateDetailsActivity.class, "OrderListVO", data);
                    }
                });
                break;
            case 24: // 已取消
                // 左边按钮
                itemOrderTvLeftButton.setVisibility(View.GONE);
                // 右边按钮
                itemOrderTvRightButton.setText("删除订单");

                itemOrderTvRightButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        orderNo = data.getOrderNo();
                        showTipsDialog();
                    }
                });
                break;
        }
    }

    /**
     * 确定删除订单弹出框
     */
    private void showTipsDialog() {
    }


    protected void showToast(String strMsg) {
        ToastUtil.showToast(getContext(), strMsg);

    }
}
